﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_
{
    public partial class ThayDoiMatKhau : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        protected void cmdSave_OnClick(object sender, EventArgs e)
        {
            CanBo ent_CanBo = DataRepository.CanBoProvider.GetByMaCanBo(Common.GetKeyUserInfo("Id"));
            if (ent_CanBo != null)
            {
                if (txtMatKhau.Text != ent_CanBo.MatKhau)
                {
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI,
                        Constant.MATKHAU_THATBAI, true);
                    return;
                }
                else
                {
                    if (txtMatKhauMoi.Text != txtMatKhauXacNhan.Text)
                    {
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI,
                         Constant.MATKHAUXACNHAN_THATBAI, true);
                        return; 
                    }
                    else
                    {
                        string query = string.Empty;
                        conn.Open();
                        query = "UPDATE CanBo SET MatKhau = "+txtMatKhauMoi.Text+" where MaCanBo = '" + Common.GetKeyUserInfo("Id") + "'";
                        SqlCommand sqlCommand = new SqlCommand();
                        sqlCommand.Connection = conn;
                        sqlCommand.CommandText = query;
                        sqlCommand.ExecuteNonQuery();
                        conn.Close();
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG,
                      Constant.THANHCONG, true);
                    }
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI,
                      Constant.THATBAI, true);
                return;
            }
        }
    }
}